# coding=utf-8
 
import json
from urllib import parse

import requests
from lxml import etree
import httpx


# 开启session，省得手动存cookie
session = requests.session()

# 关键字
keyword = '湖州'

print(keyword)

# url
douyin_host = "https://www.douyin.com"

douyin_path = f"/aweme/v1/web/search/item/?device_platform=webapp&aid=6383&channel=channel_pc_web&search_channel=aweme_video_web&sort_type=0&publish_time=0&keyword={keyword}&search_source=normal_search&query_correct_type=1&is_filter_search=0&offset=0&count=24&version_code=160100&version_name=16.1.0&cookie_enabled=true&screen_width=1280&screen_height=800&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Mozilla&browser_version=5.0+(Macintosh%3B+Intel+Mac+OS+X+10_15_5)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F90.0.4430.212+Safari%2F537.36+Edg%2F90.0.818.62&browser_online=true&_signature=_02B4Z6wo00f01-IzRoAAAIDB0mVxFRAGCIfiM0IAAJm1a3"


# douyin_url = "https://www.douyin.com/aweme/v1/web/search/item/?device_platform=webapp&aid=6383&channel=channel_pc_web&search_channel=aweme_video_web&sort_type=0&publish_time=0&keyword=%E6%B9%96%E5%B7%9E&search_source=normal_search&query_correct_type=1&is_filter_search=0&offset=0&count=24&version_code=160100&version_name=16.1.0&cookie_enabled=true&screen_width=1280&screen_height=800&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Mozilla&browser_version=5.0+(Macintosh%3B+Intel+Mac+OS+X+10_15_5)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F90.0.4430.212+Safari%2F537.36+Edg%2F90.0.818.62&browser_online=true&_signature=_02B4Z6wo00d01kljx8QAAIDAeTXwUY-HfVZJY8NAAPOO4e"

# 请求头信息
headers = {
    'Host': 'www.douyin.com',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:92.0) Gecko/20100101 Firefox/92.0',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Accept-Encoding': 'gzip, deflate, br',
    'Referer': f'https://www.douyin.com/search/{keyword}?enter_from=main_page&aid=0054cb46-8abb-40c3-823e-1bd0b165c98c&source=normal_search',
    'withCredentials': 'true',
    'Connection': 'keep-alive',
    'Cookie': 'ttwid=1%7C1NiuJNCNVrdsCb-9VvB1RhSrNj4WzVzd34mVui4Vyeo%7C1629962617%7Ce4cd7cf4ad42366ffe00397ab0c486249bbda10b8006cfb4821ba49ee91a688c; douyin.com; MONITOR_WEB_ID=a2559f0f-2c1b-496e-9320-78903b8a096b; odin_tt=7ff07807f3168da75df6045042f6c1cd0645188d571261e18fb573773a0438fad677c98fac29dc82dd077b12fc5aa7913a4f1b2a332eb9939680f2c441f75c98; s_v_web_id=verify_ksslo05e_qeCE4ZYd_rxHA_4lXr_9PpA_q8XW1E3HFDVs; passport_csrf_token_default=fea65f496902a54ae02be33f3561de35; passport_csrf_token=fea65f496902a54ae02be33f3561de35',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'no-cors',
    'Sec-Fetch-Site': 'same-origin',
    'TE': 'trailers',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'
}


# from hyper.contrib import HTTP20Adapter

# from hyper import HTTP20Connection

# session.mount(douyin_host, HTTP20Adapter())

# with HTTP20Connection(host='www.douyin.com', secure=True) as conn:
#     req = conn.request(
#             "GET",
#             douyin_path,
#             headers=headers
#         )

#     rsp = conn.get_response(req)
#     print(rsp.read())

from requests.packages import urllib3
urllib3.disable_warnings()

# get请求
response_body = session.get(douyin_host+douyin_path, headers=headers, verify=False)

raise_for_status = response_body.status_code
print(raise_for_status)

# # 获取源码信息
response_json = str(response_body.text)
print(response_json, 11111)






















